﻿Imports Sistema_InformacionNaviera.BC
Imports Sistema_InformacionNaviera.BE
Imports Sistema_InformacionNavier.UTIL


Public Class Consultar_BillOfLading_Impo

    'Inherits System.Web.UI.Page
    Inherits PaginaBase

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        Session("FlagBL") = 0 ' si es importacion 

        '---------------------------------
        'rbListado.SelectedIndex = 0
        'sTab.TabPages.Item(0).Visible = True
        'sTab.TabPages.Item(1).Visible = False


        If Not Page.IsPostBack Then
            txtNave.Text = "" : txtNroBl.Text = "" : txtCodPto.Text = "" : lblKvje.Text = 0
            txtDesgloseBLHijo.Enabled = False
            CargarGridConsulta_BillOflading()

        End If



        If My.Request.Params("__EVENTTARGET") = "MiFuncionPaquetes" Then
            CargarDatosViajePopUp()
        End If

        If My.Request.Params("__EVENTTARGET") = "MiFuncionPuertosOrigen" Then
            If Session("BPCodPuerto") = Nothing Then
                txtPto.Text = String.Empty
            Else
                txtCodPto.Text = Session("BPCodPuerto")
                txtPto.Text = Session("BPDesPuerto")

            End If
        End If


    End Sub


    Public Sub CargarDatosViajePopUp()

        If Session("BPCodViaje") = Nothing Then
            txtNave.Text = String.Empty
            txtDescripcionNave.Text = String.Empty
        Else
            txtNave.Text = Session("BPCodViaje")
            txtDescripcionNave.Text = Session("BPDesViaje")
        End If
    End Sub

    'Public Sub CargarGridConsulta_Desglose()

    '    Dim objBlImpoBE As New BillOfLadingImpoBE
    '    Dim lstBlImpoBE As New List(Of BillOfLadingImpoBE)
    '    Dim objUtil As New Funciones
    '    Dim objBlImpoBC As New BillOfLadingImpoBC
    '    objBlImpoBE.KVJE = If(txtDesgloseNave.Text = String.Empty, 0, CType(txtDesgloseNave.Text, Integer))
    '    objBlImpoBE.Csne = txtDesgloseConsignatario.Text.Trim
    '    'objBlImpoBE.
    '    objBlImpoBE.Bli_Flag = Session("FlagBL")

    '    lstBlImpoBE = objBlImpoBC.ListarBlImpo(objBlImpoBE)
    '    dgBlImportacion.DataSource = lstBlImpoBE
    '    dgBlImportacion.DataBind()
    'End Sub

    Public Sub CargarGridConsulta_BillOflading()
        Dim objBlImpoBE As New BillOfLadingImpoBE
        Dim lstBlImpoBE As New List(Of BillOfLadingImpoBE)
        Dim objUtil As New Funciones
        Dim objBlImpoBC As New BillOfLadingImpoBC
        objBlImpoBE.KVJE = If(txtNave.Text = String.Empty, 0, CType(txtNave.Text, Integer))
        objBlImpoBE.Ptoo = txtCodPto.Text.Trim
        objBlImpoBE.Nume = txtNroBl.Text.Trim

        ' Importacion = 0 
        ' Exportacion = 1

        If Session("FlagBL") = Nothing Then
            Dim iTipo As Integer = cbTipo.SelectedIndex
            objBlImpoBE.Bli_Flag = iTipo
        Else
            objBlImpoBE.Bli_Flag = Session("FlagBL")
        End If

        Dim dt As DataTable = Nothing
        Dim sMensaje As String = ""
        Dim intI As Integer = 0
        lstBlImpoBE = objBlImpoBC.ListarBlImpo(objBlImpoBE, dt, sMensaje)

        If sMensaje <> "0" Then
            If dt.Rows.Count > 0 Then

                Session("Tipo_Impo_Modif") = dt.Rows(intI).ItemArray(15).ToString()

                dgBlImportacion.DataSource = lstBlImpoBE
                dgBlImportacion.DataBind()

            Else
                dgBlImportacion.DataSource = Nothing
                dgBlImportacion.DataBind()
            End If

        Else
            dgBlImportacion.DataSource = Nothing
            dgBlImportacion.DataBind()
        End If


    End Sub



    Private Sub dgBlImportacion_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles dgBlImportacion.RowCommand

        'If Val(lblKvje.Text) > 0 Then

        Dim sMensaje As String = String.Empty
        Dim bExito As Boolean
        Dim sCadenaBL As String = Convert.ToString(e.CommandArgument)
        Dim iKvje As Integer
        Dim iKbli As Integer

        iKvje = sCadenaBL.Substring(0, InStr(sCadenaBL, "|") - 1)
        iKbli = sCadenaBL.Substring(InStr(sCadenaBL, "|"), Len(sCadenaBL) - InStr(sCadenaBL, "|"))


        Session("Desglose_iKvje") = iKvje
        Session("Desglose_iKbli") = iKbli


        Select Case e.CommandName  '.ToLower
            Case "Editar"

                Session("TipoTransaccionBLImpo") = 1 ' modificar
                Session("CodigBL") = iKbli
                Session("CodigVje") = iKvje
                Response.Redirect("BillOfLading.aspx", True)
                'Session("Tipo_BillofLading") = cbTipo.SelectedIndex

            Case "Eliminar"

                Dim objBlImpoBE As New BillOfLadingImpoBE
                objBlImpoBE.KVJE = iKvje
                objBlImpoBE.KBLM = iKbli

                Dim objPaqueteBC As New BillOfLadingImpoBC

                objPaqueteBC.EliminarBlImpo(objBlImpoBE, bExito, sMensaje)
                If bExito Then
                    Alerta("El registro fue eliminado con éxito.", False, String.Empty)
                    CargarGridConsulta_BillOflading()
                End If

            Case "Desglose"

                'Session("TipoTransaccionBLImpo_Desglose") = 2 ' desglose
                Session("CodigBL") = iKbli
                Session("CodigVje") = iKvje
                Response.Redirect("GestionarDesglose.aspx", True)

        End Select
        'End If

    End Sub

    Protected Sub btnConsultar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnConsultar.Click


        If chkBLHijo.Checked = True Then
            CargarGridConsulta_Desglose()
        Else
            CargarGridConsulta_BillOflading()
        End If


        'If cbTipo.SelectedItem.Value = 0 Then

        '    'Else
        '    '    CargarGridConsulta_Desglose()
        'End If

    End Sub

    Protected Sub btnNuevo1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNuevo.Click


        'ELIMINAR TABLAS TEMPORALES 
        '----------------------------------------------------------
        Session("tbTempMercaderia") = Nothing
        Session("tbTempFlete") = Nothing
        Session("tbTempContenedor") = Nothing
        '----------------------------------------------------------
        Session("TipoTransaccionBLImpo") = 0 ' si es nuevo

        ' elelgir una nave/ viaje
        '----------------------------------------------------------------------------
        If txtNave.Text.Trim = String.Empty Then
            'MsgBox("Seleccione una nave/viaje.", True, String.Empty)
            MsgBox("Seleccione una nave/viaje.", MsgBoxStyle.Information, "")
            'Session("FlagBL") = 0 ' si es importacion 

            'Dim alertScript As String = "<script language=JavaScript type=text/javascript"
            'alertScript += "alert('" & "Seleccione una nave/viaje" & "');"
            'alertScript += "</script" & "> "

            'Alerta("Debe ingresar el Supervisor", False, String.Empty)




        Else
            Session("CodNaveViajeBLImpo") = txtNave.Text.Trim
            Session("DesNaveViajeBLImpo") = txtDescripcionNave.Text.Trim

            '0 = importacion 
            '1 = Exportacion

            Session("Tipo_BillofLading") = cbTipo.SelectedIndex

            Response.Redirect("BillOfLading.aspx", True)
        End If

        '---------------------------------------------------------------------------

        'Session("tbTempFleteTipoTransaccion") = 0 ' si es nuevo
        '----------------------------------------------------------------------------
    End Sub



    Protected Sub btnLimpiar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLimpiar.Click
        txtNave.Text = String.Empty
        txtDescripcionNave.Text = String.Empty
        txtCodPto.Text = String.Empty
        txtPto.Text = String.Empty
        txtDesgloseBLHijo.Text = String.Empty
        txtNroBl.Text = String.Empty
        ''chkBLHijo.Checked = False

        If chkBLHijo.Checked = True Then
            CargarGridConsulta_Desglose()
        Else
            CargarGridConsulta_BillOflading()
        End If

    End Sub

   


    Sub Cambiarpagina(ByVal sender As Object, ByVal e As GridViewPageEventArgs)

        Dim objBlImpoBE As New BillOfLadingImpoBE
        Dim lstBlImpoBE As New List(Of BillOfLadingImpoBE)
        Dim objUtil As New Funciones
        Dim objBlImpoBC As New BillOfLadingImpoBC
        objBlImpoBE.KVJE = If(txtNave.Text = String.Empty, 0, CType(txtNave.Text, Integer))
        objBlImpoBE.Ptoo = txtNroBl.Text.Trim
        objBlImpoBE.Nume = txtCodPto.Text.Trim
        objBlImpoBE.Bli_Flag = Session("FlagBL")

        Dim dt As DataTable = Nothing
        Dim sMensaje As String = ""

        lstBlImpoBE = objBlImpoBC.ListarBlImpo(objBlImpoBE, dt, sMensaje)
        dgBlImportacion.PageIndex = e.NewPageIndex
        dgBlImportacion.DataSource = lstBlImpoBE
        dgBlImportacion.DataBind()


    End Sub


    'Protected Sub ASPxComboBox1_ItemRequestedByValue(ByVal source As Object, ByVal e As DevExpress.Web.ASPxEditors.ListEditItemRequestedByValueEventArgs) Handles cbTipo.ItemRequestedByValue
    '    If Page.IsPostBack Then
    '        If cbTipo.SelectedItem.Value = 1 Then
    '            sTab.TabPages.Item(0).Enabled = True
    '            sTab.TabPages.Item(1).Enabled = False
    '        Else
    '            sTab.TabPages.Item(0).Enabled = False
    '            sTab.TabPages.Item(1).Enabled = True
    '        End If
    '    End If

    'End Sub

    'Protected Sub ASPxComboBox1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles cbTipo.Load

    '    If Page.IsPostBack Then

    '        If cbTipo.SelectedItem.Value = 1 Then
    '            sTab.TabPages.Item(0).Enabled = False
    '            sTab.TabPages.Item(1).Enabled = True
    '        Else
    '            sTab.TabPages.Item(0).Enabled = True
    '            sTab.TabPages.Item(1).Enabled = False
    '        End If
    '    End If
    'End Sub


    Protected Sub ASPxCheckBox1_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
        If Page.IsPostBack Then
            If chkBLHijo.Checked = True Then
                txtDesgloseBLHijo.Enabled = True
            Else
                txtDesgloseBLHijo.Enabled = False
            End If
        End If
    End Sub

    Protected Sub chkBLHijo_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
        If Page.IsPostBack Then
            If chkBLHijo.Checked = True Then
                txtDesgloseBLHijo.Enabled = True
            Else
                txtDesgloseBLHijo.Enabled = False
            End If
        End If
    End Sub

    Protected Sub cbTipo_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbTipo.SelectedIndexChanged
        If cbTipo.SelectedIndex = 0 Then
            Session("FlagBL") = 0  ' Importacion
        Else
            Session("FlagBL") = 1 ' Exportacion
            dgBlImportacion.Columns(15).Visible = False
        End If

        CargarGridConsulta_BillOflading()
        Session("FlagBL") = Nothing

    End Sub




    Protected Sub chkBLHijo_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chkBLHijo.CheckedChanged


        If Page.IsPostBack Then
            If chkBLHijo.Checked = True Then
                txtDesgloseBLHijo.Enabled = True
                dgBlImportacion.Visible = False
                dgBlDesglose.Visible = True
                cbTipo.SelectedIndex = 0
                cbTipo.Enabled = False

                CargarGridConsulta_Desglose()

            Else
                cbTipo.SelectedIndex = 0
                cbTipo.Enabled = True
                txtDesgloseBLHijo.Text = String.Empty
                txtDesgloseBLHijo.Enabled = False
                dgBlImportacion.Visible = True
                dgBlDesglose.Visible = False
            End If
        End If


        'If chkBLHijo.Checked = False Then
        '    dgBlImportacion.Visible = True
        '    dgBlDesglose.Visible = False
        'Else
        '    dgBlImportacion.Visible = False
        '    dgBlDesglose.Visible = True
        '    CargarGridConsulta_Desglose()

        'End If


    End Sub

    Public Sub CargarGridConsulta_Desglose()

        Dim objBlImpo_DesgloseBE As New DesgloseBE
        Dim lstBlImpo_DesgloseBE As New List(Of DesgloseBE)
        Dim objUtil As New Funciones
        Dim objBlImpo_DesgloseBC As New DesgloseBC
        Dim BlHijo As String = txtDesgloseBLHijo.Text

        objBlImpo_DesgloseBE.Kvje = If(txtNave.Text = String.Empty, 0, CType(txtNave.Text, Integer))
        objBlImpo_DesgloseBE.Nume = txtNroBl.Text.Trim
        objBlImpo_DesgloseBE.Ptoo = txtCodPto.Text.Trim
        objBlImpo_DesgloseBE.KBld = If(BlHijo = String.Empty, 0, CType(BlHijo, Integer))
        objBlImpo_DesgloseBE.Flag = 0

        Dim dt As DataTable = Nothing
        dt = objBlImpo_DesgloseBC.Lista_BL_Desgloses(objBlImpo_DesgloseBE)

        dgBlDesglose.DataSource = dt
        dgBlDesglose.DataBind()

    End Sub
     

    Protected Sub btnAsignaDias_Click(sender As Object, e As EventArgs) Handles btnAsignaDias.Click
        If txtNave.Text.Length > 0 And txtDescripcionNave.Text.Length > 0 Then
            If MsgBox("Este proceso demorará. Desea continuar?", vbYesNo, "Confirmación de Asignación de Días libres") = MsgBoxResult.Yes Then

            End If
        End If
    End Sub
End Class